package com.atwisdom.star.core.dao.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.atwisdom.star.exception.pojo.BusinessException;
import lombok.val;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

public class DaoUtil {
    /**
     * 字段类型转换
     *
     * @param data      要转换数据
     * @param fieldType 要转换的数据类型
     * @return 返回参数
     * @throws ParseException 异常信息
     */
    public static Object ConvertFieldData(Object data, String fieldType) {
        String newData = String.valueOf(data);
        if (data == null || StrUtil.isBlank(newData) || StrUtil.isBlank(fieldType)) {
            return data;
        }
        String type = fieldType.toLowerCase();
        Object dataValue;
        switch (type) {
            case "double":
                dataValue = new BigDecimal(newData);
                break;
            case "date":
            case "datetime":
                if(StringUtils.isNotBlank(newData) && newData.length() == 10){
                    dataValue = DateTime.of(newData, "yyyy-MM-dd").toString("yyyy-MM-dd HH:mm:ss");
                }else{
                    dataValue = data;
                }
                break;
            case "boolean":
                dataValue = Boolean.valueOf(newData);
                break;
            case "int":
            case "integer":
                int pos = newData.indexOf(".");
                if (pos != -1) {
                    newData = newData.substring(0, pos);
                }
                dataValue = Integer.valueOf(newData);
                break;
            default:
                dataValue = data;
                break;
        }
        return dataValue;
    }
}
